home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Utilities / System 7 / ctc 1.5 / ctc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-04  |  5.3 KB  |  235 lines  |  [TEXT/KAHL]

  1. pascal Boolean GenericFilter();
  2. void ConvertFile (char *theFile, int EOLmode);
  3.  
  4. #include "ctc.h"
  5.  
  6. /* Globals, used across procedures */
  7. DialogPtr    dPtr;
  8.  
  9. /* from tickle.c */
  10. OSErr TickleParent( FSSpec *);
  11. OSErr MakeWDSpec( FSSpec *, short, ConstStr255Param );
  12.  
  13.  
  14. /*
  15. >>    ChangeTypeCreator.c    
  16. >>   change the file type and creator of any file dropped onto this program.
  17. >>     Prompts for the file type and creator, and changes all files dropped at
  18. >>   the same time to the same type and creator.
  19. >>    
  20. >>    Written by Brian Bechtel, based on code by Juri Munkki
  21. >>    Feel free to use the code in your programs.
  22. */
  23.  
  24. void    ReadFinderStuff(type, creator, EOLmode)
  25. long    type;
  26. long    creator;
  27. int        EOLmode;
  28. {
  29.     short        message,count,i;
  30.     AppFile        thefile;
  31.     FileParam    block;
  32.     FSSpec        spec;
  33.     Str255        oldName;
  34.     short        oldVol;
  35.     
  36.     CountAppFiles(&message,&count);
  37.     if(message==1)
  38.     {    return;
  39.     }
  40.     else
  41.     {    for(i=1;i<=count;i++)
  42.         {    GetAppFiles(i,&thefile);
  43.  
  44.             block.ioCompletion=0;
  45.             block.ioNamePtr=thefile.fName;
  46.             block.ioVRefNum=thefile.vRefNum;
  47.             block.ioFVersNum=thefile.versNum;
  48.             block.ioFDirIndex=0;
  49.  
  50.             /* 
  51.              * modify the EOL characters in the file
  52.              */
  53.             if (EOLmode != IGNORE)
  54.             {
  55.                 GetVol (oldName, &oldVol);
  56.                 SetVol (0, thefile.vRefNum);
  57.                 p2cstr(thefile.fName);
  58.                 ConvertFile ((char *) thefile.fName, EOLmode);
  59.                 c2pstr((char *) thefile.fName);
  60.                 SetVol (0, oldVol);
  61.             }
  62.             if (PBGetFInfo((ParmBlkPtr)&block,0) == noErr)
  63.             {
  64.                 block.ioFlFndrInfo.fdType = type;
  65.                 block.ioFlFndrInfo.fdCreator = creator;
  66.                 PBSetFInfo((ParmBlkPtr)&block,0);
  67.                 MakeWDSpec(&spec, thefile.vRefNum, thefile.fName );
  68.                 TickleParent(&spec);
  69.             }
  70.         }
  71.     }
  72. }
  73.  
  74. /*
  75. ** I put this call in a separate procedure to emphasize the fact that I'm
  76. ** modifying a global variable, dPtr, with the side effect of putting a dialog
  77. ** on the screen.  I don't want to display the dialog if we just double-click
  78. ** on the application, so I'm calling this routine at a wierd time.
  79. */
  80. DisplayDialog()
  81. {
  82.     dPtr = GetNewDialog(128, nil, (WindowPtr) -1);
  83. }
  84.  
  85. void PopRadioButton (int button)
  86. {
  87.     Handle            itemHandle;
  88.     short            itemType;
  89.     Rect            box;
  90.     ControlHandle    theControl;
  91.     Point            thePoint;
  92.  
  93.     GetDItem(dPtr, button, &itemType, &itemHandle, &box);
  94.     thePoint.v = box.top;
  95.     thePoint.h = box.left;
  96.     FindControl (thePoint, dPtr, &theControl);
  97.     SetCtlValue(theControl, false);
  98. }
  99.  
  100. void PushRadioButton (int button)
  101. {
  102.     Handle            itemHandle;
  103.     short            itemType;
  104.     Rect            box;
  105.     ControlHandle    theControl;
  106.     Point            thePoint;
  107.  
  108.     PopRadioButton (DOS);
  109.     PopRadioButton (IGNORE);
  110.     PopRadioButton (MAC);
  111.     PopRadioButton (UNIX);
  112.     GetDItem(dPtr, button, &itemType, &itemHandle, &box);
  113.     thePoint.v = box.top;
  114.     thePoint.h = box.left;
  115.     FindControl (thePoint, dPtr, &theControl);
  116.     SetCtlValue(theControl, true);
  117. }
  118.  
  119. /*
  120. ** Check to see if we were opened from the Finder by counting the passed in
  121. ** app files.  If we were opened by the finder, set the default type and
  122. ** creator to the first file's type and creator.  If not, we'll default to
  123. ** whatever is in our dialog box.
  124. */
  125. Boolean    GetTypeCreatorEOL()
  126. {
  127.     long        type;
  128.     long        creator;
  129.     short        message;
  130.     short        count;
  131.     AppFile        thefile;
  132.     FileParam    block;
  133.     short        itemHit;
  134.     Handle        itemHandle;
  135.     short        itemType;
  136.     Rect        box;
  137.     Str255        itemString;
  138.     
  139.     CountAppFiles(&message,&count);
  140.     if(count==0)
  141.         return(false);
  142.     else
  143.     {    GetAppFiles(1,&thefile);    /* get Type & Creator of 1st file. */
  144.  
  145.         block.ioCompletion=0;
  146.         block.ioNamePtr=thefile.fName;
  147.         block.ioVRefNum=thefile.vRefNum;
  148.         block.ioFVersNum=thefile.versNum;
  149.         block.ioFDirIndex=0;
  150.  
  151.         if (PBGetFInfo((ParmBlkPtr)&block,0) == noErr)
  152.         {
  153.             type = block.ioFlFndrInfo.fdType;
  154.             creator = block.ioFlFndrInfo.fdCreator;
  155.  
  156.             DisplayDialog();
  157.             
  158.             itemString[0] = 4;    /* establish artificial length */
  159.             GetDItem(dPtr, TYPE, &itemType, &itemHandle, &box);
  160.             BlockMove(&type, (Ptr)&itemString[1], 4);
  161.             SetIText(itemHandle, itemString);
  162.             GetDItem(dPtr, CREATOR, &itemType, &itemHandle, &box);
  163.             BlockMove(&creator, (Ptr)&itemString[1], 4);
  164.             SetIText(itemHandle, itemString);
  165.             PopRadioButton (DOS);
  166.             PushRadioButton (IGNORE);
  167.             PopRadioButton (MAC);
  168.             PopRadioButton (UNIX);
  169.             return(true);
  170.         }
  171.     }
  172.     return(false);
  173. }
  174.  
  175. Boolean    AskNewTypeCreatorEOL(type, creator, EOLmode)
  176. long *type;
  177. long *creator;
  178. int     *EOLmode;
  179. {
  180.     short        itemHit;
  181.     Handle        itemHandle;
  182.     short        itemType;
  183.     Rect        box;
  184.     Str255        itemString;
  185.  
  186.     SelIText(dPtr, CREATOR, 0, 4);
  187.     do {
  188.         ModalDialog(GenericFilter, &itemHit);
  189.         switch (itemHit) {
  190.             case DOS:
  191.             case IGNORE:
  192.             case MAC:
  193.             case UNIX:
  194.                 PushRadioButton (*EOLmode = itemHit);
  195.                 break;
  196.         }
  197.     } while ((itemHit != OK) && (itemHit != CANCEL));
  198.     
  199.     if (itemHit == CANCEL)
  200.         return (false);
  201.     else {
  202.         GetDItem(dPtr, TYPE, &itemType, &itemHandle, &box);
  203.         GetIText(itemHandle, itemString);
  204.         BlockMove((Ptr)&itemString[1], type, 4);
  205.         GetDItem(dPtr, CREATOR, &itemType, &itemHandle, &box);
  206.         GetIText(itemHandle, itemString);
  207.         BlockMove((Ptr)&itemString[1], creator, 4);
  208.         return (true);
  209.     }
  210. }
  211.  
  212. void    HandleUpdates()
  213. {
  214. /*    Used to handle window updates for windows other than modal dialogs.*/
  215. /*    We don't have any, so return without doing anything.*/
  216. }
  217.  
  218. void    main()
  219. {
  220.     long    type;
  221.     long    creator;
  222.     int        EOLmode;
  223.     
  224.     InitGraf(&thePort);
  225.     InitFonts();
  226.     InitWindows();
  227.     InitMenus();
  228.     TEInit();
  229.     InitCursor();
  230.     InitDialogs(nil);
  231.     if (GetTypeCreatorEOL())
  232.         if (AskNewTypeCreatorEOL(&type, &creator, &EOLmode))
  233.             ReadFinderStuff(type, creator, EOLmode);    
  234. }
  235.